package com.zhengxyou.jetpackdemo.lifecycle_liveData_viewModel;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.zhengxyou.jetpackdemo.R;
import com.zhengxyou.jetpackdemo.lifecycle_liveData_viewModel.lifecycle.MyConnectListener;
import com.zhengxyou.jetpackdemo.lifecycle_liveData_viewModel.lifecycle.MyConnectListenerByDefault;
import com.zhengxyou.jetpackdemo.lifecycle_liveData_viewModel.view_model.NameViewModel;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

public class LifecycleLiveDataViewModelActivity extends AppCompatActivity {
    private NameViewModel viewModel;
    private TextView tv_content;
    int one_click = 0;

    private MediatorLiveData mediatorLiveData = new MediatorLiveData();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lifecycle_live_data_view_model);

        tv_content = findViewById(R.id.tv_content);

        addLifecycleObserver();

        //通过ViewModelProviders数据模型提供者类获得ViewModel实例
        viewModel = ViewModelProviders.of(this).get(NameViewModel.class);

        viewModel.getCurrentName().observe(this, s -> tv_content.setText(s));
        viewModel.getCurrentListName().observe(this, strings -> {
            StringBuilder sb = new StringBuilder();
            for (String s : strings) {
                sb.append(s);
            }
            tv_content.setText(sb);
        });
        tv_content.setOnClickListener(v -> {
            String anotherName = "John Doe";
            viewModel.getCurrentName().setValue(anotherName);
        });

        findViewById(R.id.btn_one).setOnClickListener(v -> {
            viewModel.getCurrentName().setValue("one" + one_click);
            one_click++;
        });
        findViewById(R.id.btn_two).setOnClickListener(v -> {
            List<String> list = new ArrayList<>();
            list.add("two" + ++one_click);
            list.add("two" + ++one_click);
            list.add("two" + ++one_click);
            viewModel.getCurrentListName().setValue(list);
        });
        viewModel.getDistinctUntilChanged().observe(this, s -> {
            Log.e("TAG", "getDistinctUntilChanged==>" + s);
        });
        viewModel.getSwitchTrans().observe(this, s -> Log.e("TAG", s));

        viewModel.getTrans().observe(this, s -> Toast.makeText(getBaseContext(), s, Toast.LENGTH_LONG).show());
        viewModel.getWifiLevel(getApplicationContext()).observe(this, integer -> Log.e("TAG", integer + "Lv"));


        mediatorLiveData.addSource(viewModel.getCurrentName(), new Observer() {
            private int count = 1;

            @Override
            public void onChanged(Object o) {
                mediatorLiveData.setValue(o);
                if (count > 10) {
                    mediatorLiveData.removeSource(viewModel.getCurrentName());
                }
                count++;
            }
        });

        mediatorLiveData.addSource(viewModel.getCurrentListName(), new Observer() {
            @Override
            public void onChanged(Object o) {
                mediatorLiveData.setValue(o);
            }
        });

        mediatorLiveData.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                if (o instanceof String) {
                    Log.e("TAG", "mediatorLiveData==>" + o);
                } else if (o instanceof List) {
                    Log.e("TAG", "mediatorLiveData_list==>" + ((List) o).size());
                }
            }
        });
    }

    /**
     * 添加生命周期的观察者
     */
    private void addLifecycleObserver() {
        getLifecycle().addObserver(new MyConnectListenerByDefault());
        getLifecycle().addObserver(new MyConnectListener());

    }

    public static void start(Context context) {
        Intent starter = new Intent(context, LifecycleLiveDataViewModelActivity.class);
        context.startActivity(starter);
    }
}
